package com.itheima.tanhua.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.tanhua.api.db.UserInfoApi;
import com.itheima.tanhua.api.mongodb.UserLikeApi;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.vo.UserInfoVo;
import com.itheima.tanhua.vo.UsersCountsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UsersService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private UserLikeApi userLikeApi;

    //保存用户资料
    public void saveUserInfo(UserInfo userInfo) {

        //获取当前用户的id uid
        String redisKey = "CHECK_ID";
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(redisKey));

        userInfo.setId(uid);
        //调用userInfoApi保存用户资料  更新
        userInfoApi.updateById(userInfo);
    }

    //互相喜欢，喜欢，粉丝 - 统计
    public UsersCountsVo counts() {
        //获取当前用户的id uid
        String redisKey = "CHECK_ID";
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(redisKey));
        return userLikeApi.counts(uid);

    }

    //查询用户的资料信息
    public UserInfoVo selectUserInfo(Long userId) {
        //获取当前用户的id uid
        String redisKey = "CHECK_ID";
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(redisKey));
        //查询当前用户信息
        UserInfo userInfo = userInfoApi.selectById(uid);
        //封装结果集
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(userInfo, userInfoVo);
        userInfoVo.setAge(userInfo.getAge().toString());
        return userInfoVo;
    }
}
